
def parse_hit_points(hit_path):
    idx_2_hit_score = {}
    with open(hit_path, 'r') as fin:
        for line in fin.readlines():
            if line.startswith('frame_idx'):
                print("====== ignore ", line)
                continue

            idx,x,y,z,confidence,speed_change = [float(_) for _ in line.strip().split(",")]
            idx_2_hit_score[idx] = (x, y, z, confidence, speed_change)
    return idx_2_hit_score
            
def parse_hit_gt(gt_path):
    gt_idxes = []
    with open(gt_path, 'r') as fin:
        for line in fin.readlines():
            if line.strip():
                gt_idxes.append(int(line.strip()))
    return gt_idxes
    
if __name__ == '__main__':
    case_idx = 4
    hit_gt = 'case_'+str(case_idx)+'_hit_gt.txt'
    hit_point = 'case_'+str(case_idx)+'_hit_points.txt'
    gt_idxes = parse_hit_gt(hit_gt)
    idx_2_hit_score = parse_hit_points(hit_point)
    
    recall_direct_cnt = 0
    recall_adjacent_cnt = 0
    min_confidence = 1000
    for gt_idx in gt_idxes:
        if gt_idx in idx_2_hit_score:
            #print("=== direct recall ", gt_idx)
            recall_direct_cnt += 1
            x, y, z, confidence, speed_change = idx_2_hit_score[gt_idx]
            if confidence < min_confidence:
                min_confidence = confidence
        else:
            if ((gt_idx-1) in idx_2_hit_score) or ((gt_idx-2) in idx_2_hit_score) or ((gt_idx-3) in idx_2_hit_score) or ((gt_idx+1) in idx_2_hit_score) or ((gt_idx+1) in idx_2_hit_score) or ((gt_idx+3) in idx_2_hit_score):
                #print("=== adjacent recall ", gt_idx)
                recall_adjacent_cnt += 1
            else:
                print("+++ miss recall ", gt_idx)
            
    print("==== direct recall rate ", recall_direct_cnt/len(gt_idxes))
    print("==== adjacent recall rate ", (recall_direct_cnt+recall_adjacent_cnt)/len(gt_idxes))
    print("==== precision rate ", (recall_direct_cnt+recall_adjacent_cnt)/len(idx_2_hit_score))
    print("=== minimum confidence ", min_confidence)
